<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use think\Db;
use app\admin\model\Admin as AdminModel;

class Index extends Common
{	
    public function index()
    {	
	    // 获取表单上传文件 例如上传了001.jpg
	    $file = request()->file('logo');

	    if($file){
	    	//有上传LOGO
		    // 移动到框架应用根目录/public/uploads/ 目录下
		    $logo = $file->validate(['size'=>5242880,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public/uploads/logo','');
		    if($logo){
		        $thumb='/public/uploads/logo/'.$logo->getFilename();
                if($_POST){
					if($_POST['id'] !== ''){
						$_POST['logo'] = $thumb;
						$logourl = db('webinfo')->where('id',$_POST['id'])->value('logo');
						$res = db('webinfo')->update($_POST);
			    		if($res){
			    			//删除原来的logo图片
					    	$thumbpath=$_SERVER['DOCUMENT_ROOT'].$logourl;
					    	if(file_exists($thumbpath)){
			                	@unlink($thumbpath);
			                }

			    			$this->success('操作成功');
			    		}else{
			    			$this->error('操作失败');
			    		}
					}else{
						$_POST['logo'] = $thumb;
						$res = db('webinfo')->insert($_POST);
						if($res){
			    			$this->success('操作成功');
			    		}else{
			    			$this->error('操作失败');
			    		}
					}
				}
		    }else{
		        // 上传失败获取错误信息
		        $this->error($file->getError());
		    }
		}else{
			//没有上传LOGO
			if($_POST){
				if($_POST['id'] !== ''){
					$res = db('webinfo')->update($_POST);
		    		if($res){
		    			$this->success('操作成功');
		    		}else{
		    			$this->error('操作失败');
		    		}
				}else{
					$res = db('webinfo')->insert($_POST);
					if($res){
		    			$this->success('操作成功');
		    		}else{
		    			$this->error('操作失败');
		    		}
				}
			}
		}


		

    	$info = Db::name('webinfo')->find();
    	//$admin = Db::name('webinfo')->where('username',session('username'))->find();
    	$this->assign('info',$info);
        return view();
    }
}
